package cn.kylinos.springportal.common;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.OffsetDateTime;

/**
 * Response template for REST API
 * @author lichenghong
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RestResponse {
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ", timezone = "Asia/Shanghai")
    private OffsetDateTime timestamp;

    private String code;

    private String message;

    private Object data;

    public static RestResponse createResponse(String code, String message, Object data) {
        RestResponse response = new RestResponse();
        response.setTimestamp(OffsetDateTime.now());
        response.setCode(code);
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    public static RestResponse createSuccessResponse(Object data) {
        RestResponse response = new RestResponse();
        response.setTimestamp(OffsetDateTime.now());
        response.setCode("0");
        response.setMessage("success");
        response.setData(data);
        return response;
    }

    public static RestResponse createErrorResponse(String code, String errorMsg) {
        RestResponse response = new RestResponse();
        response.setTimestamp(OffsetDateTime.now());
        response.setCode(code);
        response.setMessage(errorMsg);
        response.setData(null);
        return response;
    }


}
